home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / kcl / kcl.lha / c / dguxfaslio.c < prev    next >
C/C++ Source or Header  |  1987-06-04  |  3KB  |  169 lines

  1. /*
  2. (C) Copyright Taiichi Yuasa and Masami Hagiya, 1984.  All rights reserved.
  3. */
  4.  
  5. /*
  6.     dguxfaslio.c
  7.     DG-SPECIFIC
  8.  
  9.     FASL loader io routines
  10. */
  11.  
  12. #include <stdio.h>
  13. #include "../h/fasl.h"
  14. #include "../h/fasl_global.h"
  15.  
  16. /* open fasl file */
  17. fasl_open(namep)
  18. char    *namep;        /* file name byte pointer */
  19. {
  20.     if (faslin != NULL) fasl_close();
  21.     faslin = fopen(namep, "r");
  22.     if (faslin == NULL) return(-1);
  23.     setbuf(faslin, faslbuff);
  24.     return(0);
  25.  
  26. }
  27.  
  28. /* close FASL file */
  29. fasl_close()
  30. {
  31.     fclose(faslin);
  32.     faslin = NULL;
  33.     return(0);
  34. }
  35.  
  36. /* get next fasl block */
  37. fasl_nblock()
  38. {
  39.     int block_len;
  40.  
  41.     fread(fas_buffp, FAS_HEADER_BLEN, 1, faslin);
  42.  
  43.     block_len = ((FAS_HDR_P)fas_buffp)->hdr_len;  /* set block len */
  44.  
  45.     /* if no block body , then return to caller */
  46.     if (block_len <= FAS_HEADER_LEN) return(0);
  47.  
  48.     /* we must read block body */
  49.     fread(fas_buffp + FAS_HEADER_BLEN, block_len * 2 - FAS_HEADER_BLEN,
  50.         1, faslin);
  51.     return(0);
  52. }
  53.  
  54. fasl_open_temp()
  55. {
  56.     int    i;
  57.  
  58.     if (fasltemp != NULL) fasl_close_temp();
  59.  
  60.     for (i = 0; (fasltempname[i]=fasltemporg[i]) != 0; i++) ;
  61.  
  62.     fasltemp = fopen(mktemp(fasltempname), "w+");
  63.     if (fasltemp == NULL) FEerror("Can't open temp file.", 0); 
  64.     setbuf(fasltemp, faslbuff1);
  65. }
  66.  
  67. fasl_close_temp()
  68. {
  69.     fclose(fasltemp);
  70.     fasltemp = NULL;
  71.     unlink(fasltempname);
  72.     return(0);
  73. }
  74.  
  75. fasl_read_temp(recno)
  76. int    recno;
  77. {
  78.     fseek(fasltemp, FAS_BUFF_LEN * recno, 0);
  79.     fread(fas_temp_buff, FAS_BUFF_LEN, 1, fasltemp);
  80.     fas_temp_curr = recno;
  81. }
  82.  
  83. fasl_write_temp()
  84. {
  85.     fseek(fasltemp, FAS_BUFF_LEN * fas_temp_curr, 0);
  86.     fwrite(fas_temp_buff, FAS_BUFF_LEN, 1, fasltemp);
  87. }
  88.  
  89. fasl_read_addr_rec(recno)
  90. int recno;
  91. {
  92.     fseek(fasltemp, FAS_BUFF_LEN * (fas_addr_rec_first + recno), 0);
  93.     fread(fas_addr_buff, FAS_BUFF_LEN, 1, fasltemp);
  94.     fas_addr_rec_curr = recno;
  95. }
  96.  
  97. fasl_write_addr_rec(recno)
  98. int recno;
  99. {
  100.     fseek(fasltemp, FAS_BUFF_LEN * (fas_addr_rec_first + recno), 0);
  101.     fwrite(fas_addr_buff, FAS_BUFF_LEN, 1, fasltemp);
  102. }
  103.  
  104. init_fasl_io()
  105. {
  106. }
  107.  
  108. static int
  109. hash(name, len)
  110. char *name;
  111. int len;
  112. {
  113.     return((name[0] + name[len-1]) % FAS_HSIZE);
  114. }
  115.  
  116. fasl_st(symp, symv)
  117. char *symp;
  118. int *symv;
  119. {
  120.     int    blen;
  121.     int    blen1;
  122.     int    h;
  123.     short    count;
  124.     struct sym_entry *e;
  125.     struct sym_header *p;
  126.     int    page_no;
  127.     char    *b;
  128.  
  129.     blen = strlen(symp);
  130.     page_no = h = hash(symp, blen);
  131.  
  132.     for (;;) {
  133.         b = faslsymbuff[page_no];
  134. /*        p = (struct sym_header *)(&(faslsymbuff[page_no][0]));    */
  135.         p = (struct sym_header *)b;
  136.         count = p->count;
  137.         page_no = p->npage;
  138.         e = (struct sym_entry *)(p + 1);
  139.         while (count-- > 0) {
  140.             blen1 = e->blength;
  141.             if (blen == blen1 &&
  142.                 strncmp(symp, (char *)(e + 1), blen) == 0) {
  143.                 *symv = e->value;
  144.                 return(0);
  145.             }
  146.             e = (struct sym_entry *)
  147.                 ((char *)(e + 1) + blen1 + (blen1 & 1));
  148.         }
  149.         if (page_no == 0) return(-1);
  150.     }
  151. }
  152.  
  153. init_faslst()
  154. {
  155.     FILE    *st;
  156.     int    i;
  157.     char    buff[BUFSIZ];
  158.  
  159.     st = fopen("entry_table", "r");
  160.     if (st == NULL) FEerror("Can't open entry_table.", 0);
  161.     setbuf(st, buff);
  162.  
  163.     for (i = 0; i < SYMBOL_TABLE_MAX; i++) {
  164.         if (fread(&(faslsymbuff[i][0]), BUFSIZ, 1, st) < 1) break;
  165.     }
  166.  
  167.     fclose(st);
  168. }
  169.